#include "MaterialEditOperation.h"


#include "Reflection/Property.h"
#include "Reflection/NativeFieldProperty.h"


void RMaterialChangeObjectPropertyOperation::SetObjectAndOffset(TSharedObjectPtr<RObject> InObject, int64_t InOffset)
{
    Object = InObject;
    Property = Object->GetClass()->FindNativeFieldPropertyByOffset(InOffset);
    assert(Property);
}

void RMaterialChangeObjectPropertyOperation::Undo(TSharedObjectPtr<RMaterialGraphModel> InGraph)
{
    Property-> NonStaticProperty->SetValue(Object.Get(),OldValue);
}

void RMaterialChangeObjectPropertyOperation::Redo(TSharedObjectPtr<RMaterialGraphModel> InGraph)
{
    Property->NonStaticProperty->SetValue(Object.Get(),NewValue);
}

bool RMaterialChangeObjectPropertyOperation::CombineTo(TSharedObjectPtr<RMaterialEditOperation> InFormerOperation)
{
    if(!bCanCombine)
    {
        return false;
    }

    if(!InFormerOperation->IsA<RMaterialChangeObjectPropertyOperation>())
    {
        return false;
    }

    RMaterialChangeObjectPropertyOperationPtr FormerChangeObjectPropertyOperation = InFormerOperation;

    if(FormerChangeObjectPropertyOperation->Object != Object
        || FormerChangeObjectPropertyOperation->Property != Property
    )
    {
        return false;
    }

    FormerChangeObjectPropertyOperation->NewValue = NewValue;

    return true;
}
